package com.cwz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwz.entity.Comment;
import com.cwz.entity.Like;
import com.cwz.entity.Post;
import com.cwz.mapper.CommentMapper;
import com.cwz.mapper.LikeMapper;
import com.cwz.mapper.PostMapper;
import com.cwz.service.PostService;
import com.cwz.vo.CommentVO;
import com.cwz.vo.PostVO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {
    
    @Autowired
    private LikeMapper likeMapper;
    
    @Autowired
    private CommentMapper commentMapper;


    @Override
    public IPage<PostVO> getList(Page<Post> page, Integer userId) {
        return baseMapper.selectPostList(page, userId);
    }
    
    @Override
    public Page<Post> getRankingList(Page<Post> page) {
        return page(page, new QueryWrapper<Post>()
            .orderByDesc("like_count * 2 + comment_count"));
    }
    
    @Override
    @Transactional
    public boolean toggleLike(Long postId, Integer userId) {
        // 检查是否已点赞
        Like like = likeMapper.selectOne(
            new QueryWrapper<Like>()
                .eq("post_id", postId)
                .eq("user_id", userId)
        );
        
        Post post = getById(postId);
        if (post == null) {
            throw new RuntimeException("内容不存在");
        }
        
        if (like == null) {
            // 添加点赞
            like = new Like();
            like.setPostId(postId);
            like.setUserId(userId);
            likeMapper.insert(like);
            
            // 更新点赞数
            post.setLikeCount(post.getLikeCount() + 1);
            updateById(post);
            return true;
        } else {
            // 取消点赞
            likeMapper.deleteById(like.getId());
            
            // 更新点赞数
            post.setLikeCount(post.getLikeCount() - 1);
            updateById(post);
            return false;
        }
    }
    
    @Override
    @Transactional
    public boolean addComment(Comment comment) {
        // 添加评论
        commentMapper.insert(comment);
        
        // 更新评论数
        Post post = getById(comment.getPostId());
        post.setCommentCount(post.getCommentCount() + 1);
        return updateById(post);
    }
    
    @Override
    public IPage<CommentVO> getCommentList(Page<Comment> page, Long postId) {
        return commentMapper.selectCommentList(page, postId);
    }
    
    @Override
    @Transactional
    public boolean deleteComment(Long commentId) {
        Comment comment = commentMapper.selectById(commentId);
        if (comment != null) {
            // 删除评论
            commentMapper.deleteById(commentId);
            
            // 更新文章评论数
            Post post = getById(comment.getPostId());
            if (post != null) {
                post.setCommentCount(post.getCommentCount() - 1);
                updateById(post);
            }
            return true;
        }
        return false;
    }
    
    @Override
    public Map<String, Object> getStats() {
        Map<String, Object> stats = new HashMap<>();

        // 获取总内容数
        stats.put("totalPosts", count());
        
        // 获取总点赞数
        stats.put("totalLikes", likeMapper.selectCount(null));
        
        // 获取总评论数
        stats.put("totalComments", commentMapper.selectCount(null));
        
        // 获取今日数据
        LocalDateTime today = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        stats.put("todayPosts", count(new QueryWrapper<Post>()
            .ge("create_time", today)));
        stats.put("todayComments", commentMapper.selectCount(new QueryWrapper<Comment>()
            .ge("create_time", today)));
        
        return stats;
    }


} 